package view;

import utils.Tools;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;

public class LoginView {

    private MainView mainView;

    private JFrame frame;

    private final int WIDTH =  600;
    private final int HEIGHT = 450;

    public LoginView(){
        frame = new JFrame();
        initView();
        frame.setTitle("学生信息管理系统");

        frame.setLayout(null);//空布局
        frame.setVisible(true);//可视
        frame.setResizable(false);//改变大小
        Tools.setPos(frame,WIDTH,HEIGHT);
        frame.validate();//使组件生效
    }
    //窗口初始化
    private void initView(){
        JPanel jPanel = new JPanel();
        jPanel.setBounds(0,0,WIDTH,HEIGHT );
        jPanel.setLayout(null);
        frame.add(jPanel);


//        //创建标签，存储图片作为背景
//        ImageIcon icon = new ImageIcon("image/1.png");
//        JLabel label = new JLabel(icon);
//        label.setBounds(0,0,WIDTH,HEIGHT);


        //3个标签，2个输入框，1个按钮
        JLabel title = new JLabel("学生信息管理系统");
        title.setFont(new Font("宋体",Font.BOLD,28));
        title.setForeground(Color.BLACK);
        title.setBounds(0,0,WIDTH,200);
        title.setHorizontalAlignment(SwingConstants.CENTER);//字体居中
        jPanel.add(title);



        //新建一个盘子，标签  账号和密码，
        JPanel jPanel2 = new JPanel();
        jPanel2.setBounds(0,200,WIDTH,30);
        jPanel2.setLayout(new FlowLayout(FlowLayout.CENTER));
        jPanel.add(jPanel2);

        JLabel zhanghao = new JLabel("账号");
        zhanghao.setFont(new Font("宋体",Font.BOLD,14));
        zhanghao.setForeground(Color.BLACK);
        jPanel2.add(zhanghao);
        JTextField jTextField = new JTextField(12);
        jPanel2.add(jTextField);
        jPanel2.setOpaque(false);//透明


        //密码
        JPanel jPanel22 = new JPanel();
        jPanel22.setBounds(0,230,WIDTH,30);
        jPanel22.setLayout(new FlowLayout(FlowLayout.CENTER));
        jPanel.add(jPanel22);

        JLabel mima = new JLabel("密码");
        mima.setFont(new Font("宋体",Font.BOLD,14));
        mima.setForeground(Color.BLACK);
        jPanel22.add(mima);
        JPasswordField JpasswordField = new JPasswordField(12);
        jPanel22.add(JpasswordField);
        jPanel22.setOpaque(false);//透明



        //登录按钮
        JPanel jPanel1 = new JPanel();
        jPanel1.setBounds(0,260,WIDTH,30);
        jPanel1.setLayout(new FlowLayout(FlowLayout.CENTER));
        jPanel.add(jPanel1);

        JButton jButton = new JButton("登录");
        jPanel1.add(jButton);
        jPanel1.setOpaque(false);//透明

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = jTextField.getText().trim();//trim()去除空格
                String password = new String(JpasswordField.getPassword()).trim();

                if (checkLogin(username, password)) {
                    frame.dispose(); // 关闭登录窗口
                    mainView = new MainView(); // 打开主界面
                } else {
                    JOptionPane.showMessageDialog(frame,
                            "用户名或密码错误！",
                            "登录失败",
                            JOptionPane.ERROR_MESSAGE);//错误类型
                }
            }
        });




        JLabel title1 = new JLabel("盗版出品");
        title1.setFont(new Font("宋体", Font.BOLD, 12));
        title1.setForeground(Color.BLACK);
        title1.setBounds(10, 350, 100, 20); // 使用固定位置
        jPanel.add(title1);


//        jPanel.add(label);//覆盖
    }

    private boolean checkLogin(String username, String password) {
        // 查看输入的用户名和密码
        System.out.println("输入的用户名：[" + username + "]");
        System.out.println("输入的密码：[" + password + "]");

        //buffer reader对象读取文件
        try (BufferedReader reader = new BufferedReader(new FileReader("resources/user.txt"))) {
            //按行读取
            String line;
            while ((line = reader.readLine()) != null) {
                // 查看读取的每一行内容
                System.out.println("读取的行：[" + line + "]");

                String[] parts = line.split(",");
                if (parts.length == 2) {
                    String storedUsername = parts[0].trim();
                    String storedPassword = parts[1].trim();

                    // 查看文件中的用户名和密码
                    System.out.println("文件中的用户名：[" + storedUsername + "]");
                    System.out.println("文件中的密码：[" + storedPassword + "]");

                    // 检查是否匹配
                    if (storedUsername.equals(username) && storedPassword.equals(password)) {
                        return true;
                    }
                }
            }
        } catch (IOException e) {
            // 修改异常处理，显示具体错误信息
            System.out.println("文件读取错误：" + e.getMessage());
            e.printStackTrace();
        }
        return false;
    }
}

